package com.zlzlib.libdialog;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;

import com.zlzlib.libdialog.databinding.DialogTipBinding;


/**
 * Created by zlz on 2018/2/11.
 * 提示弹窗
 */

public class TipDialog extends BaseDialog<DialogTipBinding> {

    private TipListener leftClick, rightClick;

    public TipDialog(Context context) {
        super(context);
        init();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_tip;
    }

    protected void initView() {
        if (isPortrait(context)) {
            setDialogWH(0.8, 0);
        } else {
            setDialogWH(0.3, 0);
        }
        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }

    protected void init() {
        mBinding.tvLeft.setOnClickListener(v -> {
            dismiss();
            if (leftClick != null) leftClick.onclick();
        });
        mBinding.tvRight.setOnClickListener(v -> {
            dismiss();
            if (rightClick != null) rightClick.onclick();
        });
    }

    public TipDialog setTitleGone() {
        mBinding.tvTitle.setVisibility(View.GONE);
        return this;
    }

    public TipDialog setMsgGone() {
        mBinding.tvMsg.setVisibility(View.GONE);
        return this;
    }

    public TipDialog addLayout(View view) {
        mBinding.linearContent.addView(view);
        return this;
    }

    public TipDialog addLayout(int id) {
        mBinding.linearContent.addView(LayoutInflater.from(context).inflate(id, null));
        return this;
    }

    public TipDialog setOneBtn() {
        mBinding.view.setVisibility(View.GONE);
        mBinding.tvLeft.setVisibility(View.GONE);
        mBinding.tvRight.setBackgroundResource(R.drawable.bg_btn_bottom);
        return this;
    }

    public TipDialog setTowBtn() {
        mBinding.view.setVisibility(View.VISIBLE);
        mBinding.tvLeft.setVisibility(View.VISIBLE);
        mBinding.tvRight.setBackgroundResource(R.drawable.bg_btn_right_bottom);
        return this;
    }

    public TipDialog setTitles(String msg) {
        mBinding.tvTitle.setText(msg);
        return this;
    }

    public TipDialog setTitles(int msg) {
        mBinding.tvTitle.setText(msg);
        return this;
    }


    public TipDialog setMsg(String msg) {
        mBinding.tvMsg.setText(msg);
        return this;
    }

    public TipDialog setMsg(int msg) {
        mBinding.tvMsg.setText(msg);
        return this;
    }

    /**
     * @param msg 特殊的文字
     */
    public TipDialog setMsg(SpannableStringBuilder msg) {
        mBinding.tvMsg.setMovementMethod(LinkMovementMethod.getInstance());
        mBinding.tvMsg.setText(msg);
        return this;
    }

    public TipDialog setLeftTextColor(int color) {
        mBinding.tvLeft.setTextColor(color);
        return this;
    }

    public TipDialog setRightTextColor(int color) {
        mBinding.tvRight.setTextColor(color);
        return this;
    }

    public TipDialog setLeftText(String s) {
        if (!TextUtils.isEmpty(s)) {
            mBinding.tvLeft.setText(s);
        }
        return this;
    }

    public TipDialog setRightText(String s) {
        if (!TextUtils.isEmpty(s)) {
            mBinding.tvRight.setText(s);
        }
        return this;
    }

    public TipDialog setLeftClick(TipListener leftClick) {
        this.leftClick = leftClick;
        return this;
    }

    public TipDialog setRightClick(TipListener rightClick) {
        this.rightClick = rightClick;
        return this;
    }
}
